package reese.think_in_java.java_21_thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * ***********************************************************
 *
 * @类名 : reese.think_in_java.java_21_thread.类名
 * @DESCRIPTION : 这家伙太懒啥都没写
 * @AUTHOR :  一个风情万种的男子
 * @DATE : 2016/5/31
 * ***********************************************************
 */
public class EvenChecker implements Runnable{

    private IntGenerator generator ;
    private final int id ;
    public EvenChecker(IntGenerator g,int ident){
        generator = g ;
        id = ident ;
    }

    public void run() {
        while (!generator.isCanceled()){
            int val = generator.next();
            if(val % 2 !=0){
                System.out.println(val +" not even !!");
                generator.cancel(); //cancels all EvenCheckers
            }
        }

    }

    // Test any type of IntGenerator
    public static void test(IntGenerator gp, int count) {
        System.out.println("Press Control-C to exit");
        ExecutorService exec = Executors.newCachedThreadPool();
        for (int i = 0 ;i<count ; i++){
            exec.execute(new EvenChecker(gp,i));
        }
        exec.shutdown();
    }

    public static  void  test(IntGenerator gp){
        test(gp,10);
    }

}
